package redis;

import com.alibaba.fastjson.JSON;
import jdbc.JdbcUtil;
import redis.clients.jedis.Jedis;
import util.JdbcQueryCallBack;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

/**
 * Created by Zhou_Bing on 2018/9/11.
 * <p>
 * 没有使用redis  与使用redis的对比
 * <p>
 * 有大量查询用redis比较好
 */
public class Contrast {

    public static void main(String[] args) {

        /**
         * 为什么我会写下面这几行代码？？？？ 见最后
         */
        JdbcUtil jdbcUtil = new JdbcUtil();
        jdbcUtil.query("select * from user where id ='PK_user1'", new JdbcQueryCallBack() {
            @Override
            public void execute(ResultSet rs) {

            }
        });
        jdbcUtil.execute("select * from user");
        Contrast contrast = new Contrast();
        contrast.redisFindUserById("PK_user1");
        contrast.jdbcFindUserById("PK_user1");
        contrast.redisAddUser(new User(UUID.randomUUID().toString().replace("-", ""), "venus", "测试人员", "天识", null));
        contrast.jdbcAddUser(new User(UUID.randomUUID().toString().replace("-", ""), "venus", "测试人员", "天识", null));

        int len=10000;

        //普通JDBC
        Long start = System.currentTimeMillis();
        for(int i=0;i<len;i++){
            contrast.jdbcFindUserById("PK_user1");
            contrast.jdbcFindUserById("PK_user2");
            contrast.jdbcFindUserById("PK_user3");
            contrast.jdbcAddUser(new User(UUID.randomUUID().toString().replace("-", ""), "venus", "测试人员", "天识", null));
        }
        System.out.println("JDBC: "+(System.currentTimeMillis() - start)+"ms");


        //使用redis缓存
        Long startTime = System.currentTimeMillis();
        for(int i=0;i<len;i++){
            contrast.redisFindUserById("PK_user1");
            contrast.redisFindUserById("PK_user2");
            contrast.redisFindUserById("PK_user3");
            contrast.redisAddUser(new User(UUID.randomUUID().toString().replace("-", ""), "venus", "测试人员", "天识", null));
        }
        System.out.println("Redis: "+(System.currentTimeMillis()-startTime)+"ms");
    }


    public User jdbcFindUserById(String id){
        final User[] user = {null};
        JdbcUtil.query("select * from user where id='" + id + "'", new JdbcQueryCallBack() {
            @Override
            public void execute(ResultSet rs) {
                if (rs != null) {
                    try {
                        while (rs.next()) {
                            user[0] = new User();
                            user[0].setId(rs.getString("id"));
                            user[0].setName(rs.getString("name"));
                            user[0].setOrgan(rs.getString("organ"));
                            user[0].setRole(rs.getString("role"));
                            user[0].setUnionid(rs.getString("unionid"));
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //System.out.println(user[0].toString());
        return user[0];
    }

    public boolean jdbcAddUser(User user) {
        String sql = "insert into user values ('" + user.getId() + "','" + user.getName() + "','" + user.getOrgan() + "','" + user.getRole() + "',NULL)";
        return JdbcUtil.execute(sql);
    }

    public User redisFindUserById(String id){
        User user = null;
        Jedis jedis = new Jedis("127.0.0.1");

        String result = jedis.get(id);
        if(result==null){
                user = jdbcFindUserById(id);
                jedis.set(id, JSON.toJSONString(user));
        }else{
            Long start = System.nanoTime();
            user = JSON.parseObject(result,User.class);
            //System.out.println("string to json spend "+(System.nanoTime()-start)+"ns");
        }
        jedis.close();
        //System.out.println(user.toString());
        return user;
    }


    public boolean redisAddUser(User user) {
        String sql = "insert into user values ('" + user.getId() + "','" + user.getName() + "','" + user.getOrgan() + "','" + user.getRole() + "',NULL)";
        boolean flag = JdbcUtil.execute(sql);
        if(flag){
            Jedis jedis = new Jedis("127.0.0.1");
            jedis.set(user.getId(),JSON.toJSONString(user));
            jedis.close();
        }
        return flag;
    }

    static class User {
        private String id;
        private String name;
        private String role;
        private String organ;
        private String unionid;

        public User() {
        }

        public User(String id, String name, String role, String organ, String unionid) {
            this.id = id;
            this.name = name;
            this.role = role;
            this.organ = organ;
            this.unionid = unionid;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public void setOrgan(String organ) {
            this.organ = organ;
        }

        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getRole() {
            return role;
        }

        public String getOrgan() {
            return organ;
        }

        public String getUnionid() {
            return unionid;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", role='" + role + '\'' +
                    ", organ='" + organ + '\'' +
                    ", unionid='" + unionid + '\'' +
                    '}';
        }
    }

    /**
     * 最前面几行代码的意义：
     * 为保证最真实的结果
     * 先将类的方法加载进JVM
     */
}